package com.shopping.dev.controller;

import com.alibaba.fastjson.JSON;
import com.shopping.dev.bo.CommonResultBO;
import com.shopping.dev.bo.UserBO;
import com.shopping.dev.common.util.CookieUtil;
import com.shopping.dev.pojo.Users;
import com.shopping.dev.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "用户信息接口", tags = { "用户信息接口" })
@RestController
@RequestMapping("passport")
public class UserController {

    @Autowired
    private UserService userService;
    @ApiOperation(value = "判断用户名是否已注册", notes = "判断用户名是否已注册", httpMethod = "GET", response = CommonResultBO.class)
    @GetMapping("/usernameIsExist")
    public CommonResultBO existUserName(String username) {

        CommonResultBO result = new CommonResultBO();
        try {


            boolean exist = userService.existUserName(username);
            if (exist) {
                result.setStatus(200);
            } else {
                result.setStatus(500);
                result.setMsg("用户已存在");
            }
        } catch (Exception e) {
            result.setStatus(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }
    @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/regist")
    public CommonResultBO regist(@RequestBody UserBO user,HttpServletRequest request, HttpServletResponse resp) {
        CommonResultBO result = new CommonResultBO();
        if (!user.getPassword().equals(user.getConfirmPassword())){
            result.setStatus(500);
            result.setMsg("密码输入不正确");
        }
        try {
            boolean exist = userService.existUserName(user.getUsername());
            if (!exist) {
                result.setStatus(500);
                result.setMsg("用户已存在");
                return result;
            }
            Users userdo = userService.regist(user);
            if (userdo != null) {
                result.setStatus(200);
            } else {
                result.setStatus(500);
                result.setMsg("失败");
            }
            CookieUtil.setCookie(request,resp,"user",JSON.toJSONString(userdo),-1,true);
        }catch (Exception e){
            result.setStatus(500);
            result.setMsg(e.getMessage());
        }

        return result;
    }
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/login")
    public CommonResultBO<UserBO> login(String username, String password, HttpServletRequest request, HttpServletResponse resp){
        CommonResultBO result = new CommonResultBO();

        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            result.setStatus(500);
            result.setMsg("用户名和密码不能为空");
        }
        try {
            UserBO userBO = userService.selectUser(username, password);
            if (userBO == null){
                result.setStatus(500);
                result.setMsg("用户名密码不正确");
                return result;
            }
            userBO.setPassword(null);
            userBO.setMobile(null);
            userBO.setEmail(null);
            result.setData(userBO);
            result.setStatus(200);

            CookieUtil.setCookie(request,resp,"user",JSON.toJSONString(userBO),-1,true);
        }catch (Exception e){
            result.setStatus(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }
    @ApiOperation(value = "用户退出", notes = "用户退出", httpMethod = "POST", response = CommonResultBO.class)
    @PostMapping("/logout")
    public CommonResultBO logout(@RequestParam String userId,HttpServletRequest request, HttpServletResponse resp){
        CommonResultBO result = new CommonResultBO();
        CookieUtil.delCookie(request,resp,"user");
        result.setStatus(200);
        return result;

    }
}
